#!/usr/bin/env python
# coding=utf-8
# __author__ = 'Yunchao Ling'

from pymongo import MongoClient
import re

pattern = re.compile("[a-z]")

HOST = '10.188.188.100'
PORT = 27017
DB_NAME = 'journal'
COLLECTION_NAME = 'journal'

connection = MongoClient(HOST, PORT)
db = connection[DB_NAME]
collection = db[COLLECTION_NAME]

results=collection.find({"Publisher.1":{"$exists":1}})
count=0

for result in results:
    id=result["_id"]
    for item in result["Publisher"]:
        m=re.search(pattern,item)
        if not m:
            collection.update_one({"_id":id},{"$set":{"UniquePublisher":item}})
            # print id
            # print item
            count+=1
            break
print count

results2=collection.find({"Publisher":{"$exists":1},"Publisher.1":{"$exists":0}})
count2=0
for result in results2:
    id=result["_id"]
    for item in result["Publisher"]:
        collection.update_one({"_id":id},{"$set":{"UniquePublisher":item}})
        count2+=1
        break
print count2

connection.close()
